package com.bt.springboot.demo.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zkx
 * @Date 2021/7/9 14:33
 */
public class Topic03 {

    public static void main(String[] args) {
        Topic03 topic03 = new Topic03();
        // "jbpnbwwd" "dvdn"
        String s = "jbpnbwwd";
        int i = topic03.lengthOfLongestSubstring(s);
        System.out.println(i);
    }

    public int lengthOfLongestSubstring(String s) {
        // 1.判断是否为空字符串
        if (s == null || "".equals(s)) {
            return 0;
        }

        int result = 0;
        int resultMax = 0;
        // 用list集合存放不重复的字符元素，list.size()为字符串长度
        List<Character> list = new ArrayList<>();
        // 2.遍历字符串
        for (int i = 0; i < s.length(); i++) {
            for (int j = i; j < s.length(); j++) {
                char c = s.charAt(j);
                // 有重复字符清空list
                if (list.contains(c)) {
                    list.clear();
                    break;
                } else {
                    list.add(c);
                }
                result = list.size();

                resultMax = result > resultMax ? result : resultMax;
            }
        }

        return resultMax;
    }
}
